package com.jiepos.mpos.biz.channel.service;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiepos.mpos.common.constant.DBConstant;
import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.common.dao.AppInfChnServiceCostDao;
import com.jiepos.mpos.common.entity.App_Inf_Chn_Service_CostEntity;
import com.jiepos.mpos.core.entity.Pagination;
import com.jiepos.mpos.core.service.BaseService;
import com.jiepos.mpos.core.util.IDFactoryUtil;
import com.jiepos.mpos.core.util.StringUtils;

/**
 *
 *
 * <p>标题: —— </p>
 * <p>模块: 通用平台</p>
 * <p>版权: Copyright (c) 2017 江苏杰博实信息技术有限公司
 * @ProjectName:  [mpos]
 * @Package com.jiepos.mpos.biz.channel.service
 * @author 余翼膑
 * @version 1.0
 * @since 2018年1月25号
 * @history（历次修订内容、修订人、修订时间等）
 */
@Service
public class ChnServiceCostService extends BaseService {
    @Autowired
    private AppInfChnServiceCostDao appInfChnServiceCostDao;


    /**
     * 列表
     * @param request
     * @param pagination
     * @param appInfChnServeiceCostEntity
     * @return
     */
    public Map getChnServiceCostList(HttpServletRequest request, Pagination pagination, App_Inf_Chn_Service_CostEntity appInfChnServeiceCostEntity) {
        StringBuffer sqlBuf = getInitSql("t1").append(",");
        sqlBuf.append(" t1.chn_service_cost_uuid as UUID, t2.service_no || '-' || t2.service_name as service_nm, ")
                .append("        t3.param_no || '-' || t3.param_name as param_nm, ")
                .append("        NVL2(t4.channel_no || t4.chn_nm,t4.channel_no || '-' || t4.chn_nm, '') as channel_nm, ")
                .append("         to_char(t1.CREDIT_RATE,'fm99999999999990.00') as CREDIT_RATE, ")
                .append("         to_char(t1.CREDIT_ATTACHFEE,'fm99999999999990.00') as CREDIT_ATTACHFEE,")
                .append("         to_char(t1.CREDIT_MINFEE,'fm99999999999990.00') as CREDIT_MINFEE,")
                .append("         to_char(t1.CREDIT_MAXFEE,'fm99999999999990.00') as CREDIT_MAXFEE, ")
                .append("        T1.CHANNEL_NO, ")
                .append("        T1.REC_ID ")
                .append("   FROM "+ DBConstant.Table.T_APP_INF_CHN_SERVICE_COST+" t1 ")
                .append("   left join "+ DBConstant.Table.T_APP_SERVICE_INF+" t2 ")
                .append("     on t1.service_uuid = t2.service_uuid ")
                .append("   left join "+ DBConstant.Table.T_APP_SERVICE_PARAM+" t3 ")
                .append("     on t1.service_param_uuid = t3.service_param_uuid ")
                .append("   left join "+DBConstant.Table.T_APP_INF_CHANNEL+" t4 ")
                .append("     on t1.channel_no = t4.channel_no ")
                .append(" where 1=1 ");
        List list = new ArrayList();

        //通道编号
        if(!StringUtils.isEmpty(appInfChnServeiceCostEntity.getChannel_no())){
            sqlBuf.append( " and T1.CHANNEL_NO like ? " );
            list.add("%" + appInfChnServeiceCostEntity.getChannel_no() + "%");

        }
        //业务名称
        if (!StringUtils.isBlank(appInfChnServeiceCostEntity.getService_uuid())) {
            sqlBuf.append( " and t1.service_uuid like ? " );
            list.add("%" + appInfChnServeiceCostEntity.getService_uuid() + "%");
        }
        //业务参数名称
        if (!StringUtils.isBlank(appInfChnServeiceCostEntity.getService_param_uuid())) {
            sqlBuf.append( " and t1.Service_param_uuid  like ? " );
            list.add("%" + appInfChnServeiceCostEntity.getService_param_uuid() + "%");
        }


        pagination.queryString = sqlBuf.toString();
        pagination = this.getOrderSql(pagination, "t1");
        return appInfChnServiceCostDao.getPaginationMap(pagination, "t1", list);
    }


    /**
     * 详情
     * @param chn_service_cost_uuid
     * @return
     */
    public Map getChnServiceCostById(String chn_service_cost_uuid) {

        if (StringUtils.isBlank(chn_service_cost_uuid) || chn_service_cost_uuid.equals("undefined")) {
            return new HashMap();
        }
        StringBuffer sqlBuf = getInitSql("t1").append(",");
        sqlBuf.append(" t1.chn_service_cost_uuid ,t2.service_no || '-' || t2.service_name as service_nm, ")
                .append("        t3.param_no || '-' || t3.param_name as param_nm, ")
                .append("        NVL2(t4.channel_no || t4.chn_nm,t4.channel_no || '-' || t4.chn_nm, '') as channel_nm, ")
                .append("        T1.service_uuid, t1.service_param_uuid,t1.rec_id,t1.channel_no, ")
                .append("        T1.CREDIT_RATE, ")
                .append("        T1.CREDIT_ATTACHFEE, ")
                .append("        T1.CREDIT_MINFEE, ")
                .append("        T1.CREDIT_MAXFEE, ")
                .append("        T1.CHANNEL_NO ")
                .append("   FROM "+ DBConstant.Table.T_APP_INF_CHN_SERVICE_COST+" t1 ")
                .append("   left join "+ DBConstant.Table.T_APP_SERVICE_INF+" t2 ")
                .append("     on t1.service_uuid = t2.service_uuid ")
                .append("   left join "+ DBConstant.Table.T_APP_SERVICE_PARAM+" t3 ")
                .append("     on t1.service_param_uuid = t3.service_param_uuid ")
                .append("   left join "+DBConstant.Table.T_APP_INF_CHANNEL+" t4 ")
                .append("     on t1.channel_no = t4.channel_no ")
                .append(" where t1.chn_service_cost_uuid = ? ");
        List list = new ArrayList();
        list.add(chn_service_cost_uuid);

        List<Map<String, Object>> results = appInfChnServiceCostDao.queryForList(getOrderSql(sqlBuf.toString(), "t1"), list);
        return results != null ? results.get(0) : new HashMap<String, Object>();
    }

    /**
     * 保存
     * @param appInfChnServeiceCostEntity
     * @param map
     */
    public void saveChnServiceCost(App_Inf_Chn_Service_CostEntity appInfChnServeiceCostEntity, String channelno, Map map) {

        //当前用户
        appInfChnServeiceCostEntity.setRec_upd_usr_id(String.valueOf(map.get(SystemConstant.SESSION_LOGIN_USER)));

        if (!StringUtils.isBlank(appInfChnServeiceCostEntity.getChn_service_cost_uuid())) {
            appInfChnServeiceCostEntity = (App_Inf_Chn_Service_CostEntity) this.saveCommonData(appInfChnServeiceCostEntity, map, "update");
            List noChangeList = new ArrayList();
            noChangeList.add("chn_service_cost_uuid");
            noChangeList.add("rec_crt_ts");
            appInfChnServeiceCostEntity.setChannel_no(channelno);
            appInfChnServiceCostDao.update(appInfChnServeiceCostEntity, noChangeList);
        } else {
            //新增
            System.out.println(channelno);
            appInfChnServeiceCostEntity.setChn_service_cost_uuid(IDFactoryUtil.getUUID());
            appInfChnServeiceCostEntity.setChannel_no(channelno);
            appInfChnServeiceCostEntity = (App_Inf_Chn_Service_CostEntity) this.saveCommonData(appInfChnServeiceCostEntity, map, "null");
            appInfChnServiceCostDao.save(appInfChnServeiceCostEntity);
        }
    }

    /**
     * 校验是否存在业务参数
     * @param service_param_uuid
     * @return
     */
    public String getServiceUuid(String service_param_uuid,String channelno,String chn_service_cost_uuid) {
        List list = new ArrayList();
        //新增
        if(StringUtils.isEmpty(chn_service_cost_uuid)){
            StringBuffer sb = new StringBuffer();
            sb.append("select t1.service_uuid from " + DBConstant.Table.T_APP_INF_CHN_SERVICE_COST)
                    .append(" t1 where t1.service_param_uuid = ? and t1.channel_no = ? ");
            list.add(service_param_uuid);
            list.add(channelno);

            List<Map<String, Object>> resultList = appInfChnServiceCostDao.queryForList(sb.toString(), list);
            if (resultList != null && resultList.size() > 0) {
                return "true";
            }
        }else{
            //修改
            StringBuffer sb = new StringBuffer();
            sb.append("select t1.service_param_uuid from " + DBConstant.Table.T_APP_INF_CHN_SERVICE_COST)
                    .append(" t1 where  t1.chn_service_cost_uuid = ?");
            list.add(chn_service_cost_uuid);
            List<Map<String, Object>> resultList = appInfChnServiceCostDao.queryForList(sb.toString(), list);
            if (resultList != null && resultList.size() > 0) {
                if(resultList.get(0).get("service_param_uuid").equals(service_param_uuid)){
                    return "false";
                }else{
                    List param = new ArrayList();
                    StringBuffer sf = new StringBuffer();
                    sf.append("select t1.service_param_uuid from " + DBConstant.Table.T_APP_INF_CHN_SERVICE_COST)
                            .append(" t1 where t1.service_param_uuid = ? and t1.channel_no = ? ");
                    param.add(service_param_uuid);
                    param.add(channelno);
                    List<Map<String, Object>> result = appInfChnServiceCostDao.queryForList(sf.toString(), param);
                    if (result != null && result.size() > 0) {
                        return "true";
                    }
                }
                return "false";
            }
            return "false";
        }
       return "false";

    }
}
